#! /bin/bash

# Hive组件管理
function hive()
{
    hiveInfo=`egrep "hive" $PLAY_HOME/frames.txt`
    hiveNode=`echo $hiveInfo | cut -d " " -f3`
    nodes=(${hiveNode//,/ })  

    case $1 in
    "start"){
        for i in ${nodes[*]}
        do
        
            nohup ssh $i "source /etc/profile && hive --service hiveserver2 & 2>&1 >/dev/null" &
            nohup ssh $i "source /etc/profile && hive --service metastore & 2>&1 >/dev/null" &
        done
        };;
    "stop"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && jps | grep RunJar | awk '{print \$1}' | xargs kill -9 "
        done
        };;
    esac
}

# Presto组件管理
function presto(){

    prestoInfo=`egrep "presto" $PLAY_HOME/frames.txt`

    presto=`echo $prestoInfo | cut -d " " -f1`
    isInstall=`echo $prestoInfo | cut -d " " -f2`
    installNode=`echo $prestoInfo | cut -d " " -f3`

    nodes=(${installNode//,/ })  

    if [[ $isInstall = "true" ]];then

    case $1 in
    "start"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && launcher start" &
        done
        };;
    "stop"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && launcher stop" &
        done
        };;
    esac

    else
        echo "presto未安装，请完成安装后再启动！"
    fi
}


# Zeppelin组件管理
function zeppelin(){
    zpInfo=`egrep "zeppelin" $PLAY_HOME/frames.txt`
    zpNode=`echo $zpInfo | cut -d " " -f3`
    nodes=(${zpNode//,/ })  

    case $1 in
    "start"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && zeppelin-daemon.sh start" &
        done
        };;
    "stop"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && zeppelin-daemon.sh stop" &
        done
        };;
    esac
}


# Kylin组件管理
function kylin(){
    klInfo=`egrep "kylin" $PLAY_HOME/frames.txt`
    klNode=`echo $klInfo | cut -d " " -f3`
    nodes=(${klNode//,/ })  

    case $1 in
    "start"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && kylin.sh start" &
        done
        };;
    "stop"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && kylin.sh stop" &
        done
        };;
    esac
}


# ES组件管理
function es()
{
    esInfo=`egrep "elasticsearch" $PLAY_HOME/frames.txt`
    isInstall=`echo $esInfo | cut -d " " -f2`
    installNode=`echo $esInfo | cut -d " " -f3`
    nodes=(${installNode//,/ })  

    case $1 in
    "start"){
        for i in ${nodes[*]}
        do
            # 使用es用户启动elasticsearch后台执行
            echo "正在启动 $i 节点"
            ssh $i "su -c \"source /etc/profile && elasticsearch -d\" -s /bin/sh es"
        done
        };;
    "stop"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && jps | grep Elasticsearch | awk '{print \$1}' | xargs kill -9 "
        done
        };;
    esac
}

# DS组件管理
function ds()
{
    dsInfo=`egrep "dolphinscheduler" $PLAY_HOME/frames.txt`
    isInstall=`echo $dsInfo | cut -d " " -f2`
    installNode=`echo $dsInfo | cut -d " " -f3`
    nodes=(${installNode//,/ })  

    case $1 in
    "start"){
        for i in ${nodes[*]}
        do
            echo "正在启动 $i 节点"
            ssh $i "source /etc/profile && bash \$DS_HOME/bin/dolphinscheduler-daemon.sh start standalone-server"
        done
        };;
    "stop"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && bash \$DS_HOME/bin/dolphinscheduler-daemon.sh stop standalone-server"
        done
        };;
    esac
}

# Zookeeper组件管理
function zookeeper()
{

    #1.在frames.txt中查看是否需要安装zk
    zkInfo=`egrep "^zookeeper" $PLAY_HOME/frames.txt`
    
    zk=`echo $zkInfo | cut -d " " -f1`
    isInstall=`echo $zkInfo | cut -d " " -f2`
    installNode=`echo $zkInfo | cut -d " " -f3`
    nodes=(${installNode//,/ })  

    case $1 in
    "start"){
        for i in ${nodes[*]}
        do
        
            nohup ssh $i "source /etc/profile && zkServer.sh start"
        done
        };;
    "stop"){
        for i in ${nodes[*]}
        do
            ssh $i "source /etc/profile && zkServer.sh stop "
        done
        };;
    esac
}


if [ "$#" -lt 2 ]; then
    echo "缺少参数，请输入需要执行的脚本和操作命令。"
    echo "示例：./脚本名.sh [脚本] [操作命令]"
    echo "eg：pl_manager hive start"
    exit 1
fi


# Hive
if [ $1 == "hive" ]; then 

    case $2 in 
        start)
        echo "执行Hive启动命令..."
        hive start
        echo "Hive已启动"
        ;;

        stop)
        echo "执行Hive停止命令..."
        hive stop
        echo "Hive已停止"
        ;;
        
        *)
        echo "无效的Hive操作命令。可用命令：start, stop"
        ;;

    esac
fi


# Presto
if [ $1 == "presto" ]; then 

    case $2 in 
        start)
        echo "执行Presto启动命令..."
        presto start
        ;;

        stop)
        echo "执行Presto停止命令..."
        presto stop
        ;;

        *)
        echo "无效的Presto操作命令。可用命令：start, stop"
        ;;

    esac
fi

# Zeppelin
if [ $1 == "zeppelin" ]; then 

    case $2 in 
        start)
        echo "执行Zeppelin启动命令..."
        zeppelin start
        ;;

        stop)
        echo "执行Zeppelin停止命令..."
        zeppelin stop
        ;;

        *)
        echo "无效的Zeppelin操作命令。可用命令：start, stop"
        ;;
    esac
fi

# Kylin
if [ $1 == "kylin" ]; then 

    case $2 in 
        start)
        echo "执行Kylin启动命令..."
        kylin start
        ;;

        stop)
        echo "执行Kylin停止命令..."
        kylin stop
        ;;

        *)
        echo "无效的Kylin操作命令。可用命令：start, stop"
        ;;
    esac
fi

# es
if [ $1 == "es" ]; then 

    case $2 in 
        start)
        echo "执行ES启动命令..."
        es start
        ;;

        stop)
        echo "执行ES停止命令..."
        es stop
        ;;

        *)
        echo "无效的ES操作命令。可用命令：start, stop"
        ;;
    esac
fi

# ds
if [ $1 == "ds" ]; then 

    case $2 in 
        start)
        echo "执行DS启动命令..."
        ds start
        ;;

        stop)
        echo "执行DS停止命令..."
        ds stop
        ;;

        *)
        echo "无效的DS操作命令。可用命令：start, stop"
        ;;
    esac
fi


# zk
if [ $1 == "zookeeper" ]; then 

    case $2 in 
        start)
        echo "执行ZK启动命令..."
        zookeeper start
        ;;

        stop)
        echo "执行ZK停止命令..."
        zookeeper stop
        ;;

        *)
        echo "无效的ZK操作命令。可用命令：start, stop"
        ;;
    esac
fi